//
//  GuidePagesView.swift
//  
//
//  Created by 马应雷 on 16/2/26.
//
//

import UIKit

public var SCREEN_WIDTH: CGFloat {
    get {
        return UIScreen.mainScreen().bounds.size.width
    }
}

public var SCREEN_HEIGHT: CGFloat {
    get {
        return UIScreen.mainScreen().bounds.size.height
    }
}

public var SCREEN_SCALE: CGFloat {
    get {
        return SCREEN_WIDTH/320
    }
}

class GuidePagesView: UIView {
    
    private var beginContentOffsetX = CGFloat(0)
    @IBOutlet private var pageControl: UIPageControl!
    @IBOutlet private var pageControl_Bottom: NSLayoutConstraint!
    @IBOutlet private var pageScrollView: UIScrollView!
    
    //page 1
    @IBOutlet private var pageOne_BgImg_1: UIImageView!
    @IBOutlet private var pageOne_BgImg_2: UIImageView!
    @IBOutlet private var pageOne_ContentImg_1: UIImageView!
    @IBOutlet private var pageOne_ContentImg_2: UIImageView!
    @IBOutlet private var pageOne_ContentImg_1_top: NSLayoutConstraint!
    
    //page 2
    private var oncePageTwo: dispatch_once_t = 0
    @IBOutlet private var pageTwoView: UIView!
    @IBOutlet private var pageTwo_BgImg: UIImageView!
    @IBOutlet private var pageTwo_BgImg_Top: NSLayoutConstraint!
    @IBOutlet private var pageTwo_BgImg_Left: NSLayoutConstraint!
    @IBOutlet private var pageTwo_BgImg_Width: NSLayoutConstraint!
    @IBOutlet private var pageTwo_BgImg_Height: NSLayoutConstraint!
    @IBOutlet private var pageTwo_ContentImg_1: UIImageView!
    @IBOutlet private var pageTwo_ContentImg_2: UIImageView!
    
    //page 3
    private var oncePageThree: dispatch_once_t = 0
    @IBOutlet private var pageThreeView: UIView!
    @IBOutlet private var pageThreeView_Left: NSLayoutConstraint!
    @IBOutlet private var pageThreeView_Right: NSLayoutConstraint!
    @IBOutlet private var pageThree_BgImg_1: UIImageView!
    @IBOutlet private var pageThree_BgImg_2: UIImageView!
    @IBOutlet private var pageThree_BgImg_3: UIImageView!
    @IBOutlet private var pageThree_BgImg_3_Top: NSLayoutConstraint!
    @IBOutlet private var pageThree_ContentImg_1: UIImageView!
    @IBOutlet private var pageThree_ContentImg_2: UIImageView!
    @IBOutlet private var pageThree_ContentImg_1_top: NSLayoutConstraint!
    
    //page 4
    private var oncePageFour: dispatch_once_t = 0
    @IBOutlet private var pageFourView: UIView!
    @IBOutlet private var pageFourView_Top: NSLayoutConstraint!
    @IBOutlet private var pageFourView_Bottom: NSLayoutConstraint!
    @IBOutlet private var pageFour_BgImg_1: UIImageView!
    @IBOutlet private var pageFour_BgImg_2: UIImageView!
    @IBOutlet private var pageFour_ContentImg_1: UIImageView!
    @IBOutlet private var pageFour_ContentImg_2: UIImageView!
    @IBOutlet private var pageFour_View: UIView!
    @IBOutlet private var pageFour_View_Right: NSLayoutConstraint!
    @IBOutlet private var pageFour_View_Left: NSLayoutConstraint!
    @IBOutlet private var pageFour_View_Top: NSLayoutConstraint!
    @IBOutlet private var pageFour_View_Bottom: NSLayoutConstraint!
    
    //page 5
    private var oncePageFive: dispatch_once_t = 0
    @IBOutlet private var pageFiveView: UIView!
    @IBOutlet private var pageFourView_Right: NSLayoutConstraint!
    @IBOutlet private var pageFourView_Left: NSLayoutConstraint!
    @IBOutlet private var pageFive_BgImg_1: UIImageView!
    @IBOutlet private var pageFive_BgImg_1_Width: NSLayoutConstraint!
    @IBOutlet private var pageFive_BgImg_1_Height: NSLayoutConstraint!
    @IBOutlet private var pageFive_BgImg_1_Bottom: NSLayoutConstraint!
    @IBOutlet private var pageFive_BgImg_2: UIImageView!
    @IBOutlet private var pageFive_ContentImg_1: UIImageView!
    @IBOutlet private var pageFive_ContentImg_1_top: NSLayoutConstraint!
    @IBOutlet private var pageFive_ContentImg_2_Bottom: NSLayoutConstraint!
    // GIF图是用了一个别人写的类，感谢开源~~
    @IBOutlet private var pageFive_RunImg: YLImageView!
    @IBOutlet private var pageFive_RunImg_Left: NSLayoutConstraint!
    @IBOutlet private var pageFive_RunImg_Bottom: NSLayoutConstraint!
    @IBOutlet private var pageFive_LoadBtn: UIButton!
    
    class func showGuidePagesView() {
        let showView = (NSBundle.mainBundle().loadNibNamed("GuidePagesView", owner: nil, options: nil) as NSArray).lastObject as! GuidePagesView
        showView.initAnimate()
        showView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
        showView.alpha = 0
        UIApplication.sharedApplication().keyWindow?.addSubview(showView)
        
        UIView.animateWithDuration(0.3) { 
            showView.alpha = 1
        }
    }
    
    private func initAnimate() {
        
        // 页面 1
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.pageControl_Bottom.constant = 5
            self.pageControl.setNeedsLayout()
            self.pageControl.layoutIfNeeded()
        })
        
        // 页面 2
        self.pageTwo_BgImg_Width.constant = SCREEN_WIDTH
        self.pageTwo_BgImg_Height.constant = 312 * SCREEN_SCALE
        
        // 页面 4
        self.pageFour_BgImg_2.transform = CGAffineTransformMakeScale(0.9,0.9)
        self.pageFour_BgImg_2.alpha = 0
        
        // 页面 5
        self.pageFive_BgImg_1_Width.constant = 650 * SCREEN_SCALE
        self.pageFive_BgImg_1_Height.constant = 650 * SCREEN_SCALE
        self.pageFive_BgImg_1_Bottom.constant = -262 * SCREEN_SCALE
        
        self.goRotation()
        
        let path = NSBundle.mainBundle().URLForResource("5_run", withExtension: "gif")
        let data = NSData(contentsOfURL: path!)
        self.pageFive_RunImg.image = YLGIFImage(data: data!)
        self.pageFive_RunImg_Left.constant = 49 * SCREEN_SCALE
        self.pageFive_RunImg_Bottom.constant = 217 * SCREEN_SCALE
    }
    
    //地球旋转
    private func goRotation() {

        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(-(M_PI * 2.0))
        rotateAnimation.duration = 40
        rotateAnimation.repeatCount = MAXFLOAT
        self.pageFive_BgImg_1.layer.addAnimation(rotateAnimation, forKey: nil)
    }
    
    //滑动页面 1 动画
    private func scrollPageOneAnimate(rate: CGFloat) {
        self.pageOne_BgImg_1.alpha = 1 - (rate*2)
        self.pageOne_BgImg_2.alpha = 1 - rate
        self.pageOne_BgImg_2.transform = CGAffineTransformMakeScale(1 - (rate * 2), 1 - (rate * 2))
        self.pageOne_ContentImg_1_top.constant = 25 + 350 * rate
        
        let pageTwoViewTransform = rate + 0.3 > 1 ? 1 : rate + 0.3
        self.pageTwoView.alpha = rate
        self.pageTwoView.transform = CGAffineTransformMakeScale(pageTwoViewTransform,pageTwoViewTransform)
    }
    
    //滑动页面 2 动画
    private func scrollPageTwoAnimate(rate: CGFloat) {
        
        let oldImg_Width  = SCREEN_WIDTH
        let oldImg_Height = 312 * SCREEN_SCALE
        let oldImg_Top    = CGFloat(100)
        let oldImg_Left   = CGFloat(0)
        
        let newImg_Width  = CGFloat(130) * SCREEN_SCALE
        let newImg_Height = CGFloat(130) * SCREEN_SCALE
        let newImg_Top    = CGFloat(140)// * SCREEN_SCALE
        let newImg_Left   = (SCREEN_WIDTH/2-(newImg_Width/2))+SCREEN_WIDTH
        
        self.pageTwo_BgImg_Width.constant  = oldImg_Width - ((oldImg_Width - newImg_Width) * rate)
        self.pageTwo_BgImg_Height.constant = oldImg_Height - ((oldImg_Height - newImg_Height) * rate)
        self.pageTwo_BgImg_Top.constant    = oldImg_Top - ((oldImg_Top - newImg_Top) * rate)
        self.pageTwo_BgImg_Left.constant   = oldImg_Left - ((oldImg_Left - newImg_Left) * rate)
        
        let pageThreeBgImgTransform = rate + 0.3 > 1 ? 1 : rate + 0.3
        self.pageThree_BgImg_1.transform = CGAffineTransformMakeScale(pageThreeBgImgTransform,pageThreeBgImgTransform)
        self.pageThree_BgImg_2.transform = CGAffineTransformMakeScale(rate,rate)
        self.pageThree_BgImg_3.transform = CGAffineTransformMakeScale(rate,rate)
        self.pageThree_BgImg_2.alpha = rate
        self.pageThree_BgImg_3.alpha = rate
        self.pageThree_BgImg_3_Top.constant = 100 - 400 * (1 - rate)
        
        self.pageThree_ContentImg_1.alpha = rate
        self.pageThree_ContentImg_2.alpha = rate
        self.pageThree_ContentImg_1_top.constant = 40 + 400 * (1 - rate)
    }
    
    //滑动页面 3 动画
    private func scrollPageThreeAnimate(rate: CGFloat) {
        
        let img_Width  = CGFloat(130) * SCREEN_SCALE
        let img_Left   = (SCREEN_WIDTH/2-(img_Width/2))+SCREEN_WIDTH
        
        self.pageTwo_BgImg_Left.constant = img_Left + SCREEN_WIDTH * rate
        self.pageTwo_BgImg_Top.constant = 140 + (110 * SCREEN_SCALE) * rate
        self.pageTwo_BgImg.alpha = 1 - rate*2
        self.pageTwo_BgImg.transform = CGAffineTransformMakeScale(1 - rate,1 - rate)
        
        self.pageThreeView.alpha = 1 - rate*2
        self.pageThreeView.transform = CGAffineTransformMakeScale(1 - rate,1 - rate)
        self.pageThreeView_Left.constant = SCREEN_WIDTH * rate
        self.pageThreeView_Right.constant = -(SCREEN_WIDTH * rate)
        
        self.pageFourView.alpha = rate
        self.pageFour_View.transform = CGAffineTransformMakeScale(rate,rate)
        self.pageFour_View_Left.constant = -(SCREEN_WIDTH * (1 - rate))
        self.pageFour_View_Right.constant = SCREEN_WIDTH * (1 - rate)
        self.pageFour_View_Top.constant = SCREEN_HEIGHT * (1 - rate)
        self.pageFour_View_Bottom.constant = -(SCREEN_HEIGHT * (1 - rate))
    }
    
    //滑动页面 4 动画
    private func scrollPageFourAnimate(rate: CGFloat) {
        
        let pageFourViewTransform = 1 - rate < 0.3 ? 0.3 : 1 - rate
        
        self.pageFour_View.transform = CGAffineTransformMakeScale(pageFourViewTransform,pageFourViewTransform)
        self.pageFour_View.alpha = 1 - rate
        self.pageFour_View_Left.constant = SCREEN_WIDTH * rate
        self.pageFour_View_Right.constant = -(SCREEN_WIDTH * rate)
        
        self.pageFiveView.alpha = rate
        self.pageFourView_Left.constant = -(SCREEN_WIDTH * (1 - rate))
        self.pageFourView_Right.constant = SCREEN_WIDTH * (1 - rate)
        self.pageFive_BgImg_1.transform = CGAffineTransformMakeScale(1+2*(1-rate),1+2*(1-rate))
        
        self.pageFive_ContentImg_1_top.constant = 100 - (500*(1 - rate))
        self.pageFive_ContentImg_2_Bottom.constant = 93 - (500*(1 - rate))
        
        self.pageFive_RunImg.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(CGFloat(CGFloat((360*(M_PI / 180.0)))*CGFloat(rate))),rate,rate)
        
        self.pageControl_Bottom.constant = 5 - (100 * rate)
    }
    
    //进入APP
    @IBAction func loadInApp(sender: UIButton) {
        
        self.pageScrollView.scrollEnabled = false
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            sender.alpha = 0
            self.pageFive_LoadBtn.alpha = 0
            self.pageFive_RunImg.alpha = 0
            self.pageFive_RunImg = nil
        })
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.pageFive_BgImg_1.transform = CGAffineTransformMakeScale(3,3)
            self.pageFive_ContentImg_1_top.constant = -400
            self.pageFive_ContentImg_1.setNeedsLayout()
            self.pageFive_ContentImg_1.layoutIfNeeded()
            
        }, completion:nil)
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(1.3 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.alpha = 0
            })
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(1.7 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
            self.removeFromSuperview()
        }
    }
}

// MARK: - UIScrollViewDelegate -
extension GuidePagesView: UIScrollViewDelegate {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        self.pageControl.currentPage = Int(scrollView.contentOffset.x/SCREEN_WIDTH)
        
        let rate = (((scrollView.contentOffset.x)%SCREEN_WIDTH)/SCREEN_WIDTH)
        
        if  rate != 0 && (scrollView.contentOffset.x) > 0 {
            
            if scrollView.contentOffset.x - beginContentOffsetX > 0 {
                //
                // 从左到右滑动
                //
                if scrollView.contentOffset.x > SCREEN_WIDTH * 3 {
                    //从第四到第五张
                    self.scrollPageFourAnimate(rate)
                } else if scrollView.contentOffset.x > SCREEN_WIDTH * 2 {
                    //从第三到第四张
                    self.scrollPageThreeAnimate(rate)
                } else if scrollView.contentOffset.x > SCREEN_WIDTH {
                    //从第二到第三张
                    self.scrollPageTwoAnimate(rate)
                } else if scrollView.contentOffset.x > 0 {
                    //从第一到第二张
                    self.scrollPageOneAnimate(rate)
                }
                
            } else {
                //
                // 从右到左滑动
                //
                if scrollView.contentOffset.x < SCREEN_WIDTH {
                    //从第二到第一张
                    self.scrollPageOneAnimate(rate)
                } else if scrollView.contentOffset.x < SCREEN_WIDTH * 2 {
                    //从第三到第二张
                    self.scrollPageTwoAnimate(rate)
                } else if scrollView.contentOffset.x < SCREEN_WIDTH * 3 {
                    //从第四到第三张
                    self.scrollPageThreeAnimate(rate)
                } else if scrollView.contentOffset.x < SCREEN_WIDTH * 4 {
                    //从第五到第四张
                    self.scrollPageFourAnimate(rate)
                }
            }
        }
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        beginContentOffsetX = scrollView.contentOffset.x
    }
}
